﻿namespace FrameWork.Lib.HTML
{
    using System;
    using System.Collections;
    using System.Reflection;

    public class HtmlNodeCollection : CollectionBase
    {
        private HtmlElement mParent;

        public HtmlNodeCollection()
        {
            this.mParent = null;
        }

        internal HtmlNodeCollection(HtmlElement parent)
        {
            this.mParent = parent;
        }

        public int Add(HtmlNode node)
        {
            if (this.mParent != null)
            {
                node.SetParent(this.mParent);
            }
            return base.List.Add(node);
        }

        public HtmlNodeCollection FindByAttributeName(string attributeName)
        {
            return this.FindByAttributeName(attributeName, true);
        }

        public HtmlNodeCollection FindByAttributeName(string attributeName, bool searchChildren)
        {
            HtmlNodeCollection nodes = new HtmlNodeCollection(null);
            foreach (HtmlNode node in base.List)
            {
                if (node is HtmlElement)
                {
                    foreach (HtmlAttribute attribute in ((HtmlElement) node).Attributes)
                    {
                        if (attribute.Name.ToLower().Equals(attributeName.ToLower()))
                        {
                            nodes.Add(node);
                            break;
                        }
                    }
                    if (searchChildren)
                    {
                        foreach (HtmlNode node2 in ((HtmlElement) node).Nodes.FindByAttributeName(attributeName, searchChildren))
                        {
                            nodes.Add(node2);
                        }
                    }
                }
            }
            return nodes;
        }

        public HtmlNodeCollection FindByAttributeNameValue(string attributeName, string attributeValue)
        {
            return this.FindByAttributeNameValue(attributeName, attributeValue, true);
        }

        public HtmlNodeCollection FindByAttributeNameValue(string attributeName, string attributeValue, bool searchChildren)
        {
            HtmlNodeCollection nodes = new HtmlNodeCollection(null);
            foreach (HtmlNode node in base.List)
            {
                if (node is HtmlElement)
                {
                    foreach (HtmlAttribute attribute in ((HtmlElement) node).Attributes)
                    {
                        if (attribute.Name.ToLower().Equals(attributeName.ToLower()))
                        {
                            if (attribute.Value.ToLower().Equals(attributeValue.ToLower()))
                            {
                                nodes.Add(node);
                            }
                            break;
                        }
                    }
                    if (searchChildren)
                    {
                        foreach (HtmlNode node2 in ((HtmlElement) node).Nodes.FindByAttributeNameValue(attributeName, attributeValue, searchChildren))
                        {
                            nodes.Add(node2);
                        }
                    }
                }
            }
            return nodes;
        }

        public HtmlNodeCollection FindByName(string name)
        {
            return this.FindByName(name, true);
        }

        public HtmlNodeCollection FindByName(string name, bool searchChildren)
        {
            HtmlNodeCollection nodes = new HtmlNodeCollection(null);
            foreach (HtmlNode node in base.List)
            {
                if (node is HtmlElement)
                {
                    if (((HtmlElement) node).Name.ToLower().Equals(name.ToLower()))
                    {
                        nodes.Add(node);
                    }
                    if (searchChildren)
                    {
                        foreach (HtmlNode node2 in ((HtmlElement) node).Nodes.FindByName(name, searchChildren))
                        {
                            nodes.Add(node2);
                        }
                    }
                }
            }
            return nodes;
        }

        public int IndexOf(HtmlNode node)
        {
            return base.List.IndexOf(node);
        }

        public void Insert(int index, HtmlNode node)
        {
            if (this.mParent != null)
            {
                node.SetParent(this.mParent);
            }
            base.InnerList.Insert(index, node);
        }

        public HtmlNode this[string name]
        {
            get
            {
                HtmlNodeCollection nodes = this.FindByName(name, false);
                if (nodes.Count > 0)
                {
                    return nodes[0];
                }
                return null;
            }
        }

        public HtmlNode this[int index]
        {
            get
            {
                return (HtmlNode) base.InnerList[index];
            }
            set
            {
                if (this.mParent != null)
                {
                    value.SetParent(this.mParent);
                }
                base.InnerList[index] = value;
            }
        }
    }
}

